const Router = require('koa-router')
const home = new Router()
const db = require('../mysql/index');

// var gamelist
//
// db.query(`SELECT game.G_NAME "gamename",game.G_COVER "url",game.G_DESC "gamedesc" FROM game`,function(result){
//     gamelist = result
// })

//h5游戏信息获取
home.get('/hgamelist', async (ctx)=>{
    ctx.body = await new Promise((resolve, reject) => {
        const sql = `SELECT game.G_ID "id", game.G_NAME "gamename",game.G_COVER "url",game.G_DESC "gamedesc",game.G_URL "gameurl",game.G_TYPE "gametype" FROM game where G_TYPE = 'H5'`
        return db.query(sql,[], (data) => {
            data.map(val => {
                val.url = 'http://localhost:5000/' + val.url
                if(val.gameurl) val.gameurl = 'http://localhost:5000/' + val.gameurl
            })
            resolve(data)
        })
    })
})

//exe游戏信息获取
home.get('/egamelist', async (ctx)=>{
    ctx.body = await new Promise((resolve, reject) => {
        const sql = `SELECT game.G_ID "id", game.G_NAME "gamename",game.G_COVER "url",game.G_DESC "gamedesc",game.G_URL "gameurl",game.G_TYPE "gametype" FROM game where G_TYPE = 'exe'`
        return db.query(sql,[], (data) => {
            data.map(val => {
                val.url = 'http://localhost:5000/' + val.url
                if(val.gameurl) val.gameurl = 'http://localhost:5000/' + val.gameurl
            })
            resolve(data)
        })
    })
})

module.exports = home
